<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/16
 * Time: 11:41
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\ArticleCategoryModel;
use app\shop\model\ArticleModel;

class Article extends Admin
{
    public function index()
    {
        $list = ArticleModel::getAllList();
        $page = $list->render();
        return ZBuilder::make('table')
            ->setPageTitle('文章列表')
            ->setTableName('article')
            ->addColumns([
                ['title','标题'],
                ['author','作者'],
                ['original_img','封面图','picture'],
                ['is_show','显示','switch'],
                ['is_rec','推荐','switch'],
                ['time','发布时间','datetime'],
                ['right_button','操作']
            ])
            ->addTopButton('add')
            ->addRightButtons('edit,delete')
            ->setRowList($list)
            ->setPages($page)
            ->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $this->store();
        }
        $cate_list = $this->getCateList();
        return ZBuilder::make('form')
            ->addFormItems([
                ['number','sort','排序','','100'],
                ['text','title','文章标题'],
                ['text','author','作者'],
                ['textarea','desc','文章简介'],
                ['select','cate_id','文章分类','',$cate_list],
                ['tags','tags','标签','标签用回车隔开'],
                ['image','original_img','封面图'],
                ['switch','is_show','显示'],
                ['switch','is_rec','推荐'],
                ['ueditor','content','内容'],
            ])
            ->setUrl(url('add'))
            ->fetch();
    }

    public function edit($id=0)
    {
        if(request()->isPost()){
            $this->store();
        }
        $data = ArticleModel::getOne($id);
        $cate_list = $this->getCateList();
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                ['number','sort','排序','','100'],
                ['text','title','文章标题'],
                ['text','author','作者'],
                ['textarea','desc','文章简介'],
                ['select','cate_id','文章分类','',$cate_list],
                ['tags','tags','标签','标签用回车隔开'],
                ['image','original_img','封面图'],
                ['switch','is_show','显示'],
                ['switch','is_rec','推荐'],
                ['ueditor','content','内容'],
            ])
            ->setUrl(url('edit'))
            ->setFormData($data)
            ->fetch();
    }


    private function store()
    {
        $id = input('param.id',0);
        $data = input('post.');
        $validate = new \app\shop\validate\Article();
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $data['is_show'] = intval(isset($data['is_show']));
        $data['is_rec'] = intval(isset($data['is_rec']));
        $res = ArticleModel::store($data, $id);
        if($res){
            $this->success('保存成功',url('index'));
        }else{
            $this->error('保存失败，请重试！');
        }
    }

    private function getCateList()
    {
        $list = ArticleCategoryModel::getAllList();
        $newList = [];
        foreach ($list as $item){
            $newList[$item['id']] = $item['name'];
        }
        return $newList;
    }
}